import 'package:flutter/cupertino.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';
import 'package:xindong/common/xindong_sezhi.dart';
import 'package:xindong/common/xindong_define.dart';
import 'package:xindong/widgets/common/xindong_app_bar_dialog.dart';
import 'package:xindong/widgets/common/xindong_app_bar_icon_button.dart';
import 'package:xindong/widgets/dialog/xindong_base_sheet.dart';

class XinDongPickerSheet extends StatefulWidget {
  final String title;
  final String selectText;
  final List<String> dataList;
  final Function(int index, String text) doneCallback;

  XinDongPickerSheet.show({
    super.key,
    required this.title,
    required this.selectText,
    required this.dataList,
    required this.doneCallback,
  }) {
    showSmartSheet(sheet: this);
  }

  @override
  State<XinDongPickerSheet> createState() => _XinDongPickerSheetState();
}

class _XinDongPickerSheetState extends State<XinDongPickerSheet> {
  final TextStyle textStyle = const TextStyle(fontSize: 20, color: DZColors.titleColor);

  late int _currentIndex = 0;

  late int weightIndex = 0;

  // 数据
  List<Widget> _widgetList() {
    List<Widget> list = [];
    for (String text in widget.dataList) {
      list.add(Center(child: Text(text, style: textStyle)));
    }
    return list;
  }

  @override
  void initState() {
    super.initState();
    _currentIndex = widget.dataList.indexOf(widget.selectText);
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: DZColors.dialog,
      padding: EdgeInsets.only(bottom: bottomBarHeight),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          XinDongDialogAppBar(
            widget.title,
            leading: XinDongAppBarIconButton(onTap: () => SmartDialog.dismiss()),
            onTapDone: () {
              widget.doneCallback(_currentIndex, widget.dataList[_currentIndex]);
              SmartDialog.dismiss();
            },
          ),
          SizedBox(
            height: 216,
            child: CupertinoPicker(
              itemExtent: kCellHeight,
              scrollController: FixedExtentScrollController(initialItem: _currentIndex),
              onSelectedItemChanged: (position) {
                _currentIndex = position;
              },
              children: _widgetList(),
            ),
          ),
        ],
      ),
    );
  }
}
